package com.corpgovernment.common.shunt;

import com.corpgovernment.common.base.BaseUserInfo;
import com.ctrip.corp.obt.generic.core.context.TenantContext;
import com.ctrip.corp.obt.generic.core.context.UserInfoContext;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author ldma
 * @date 2024/3/19
 */
@Repository
@Slf4j
public class ShuntConfigDao {

    @Value("${shuntConfig:}")
    private String shuntConfig;

    public Boolean openFeature(String featureName) {
        // 无任何配置则不放流量
        if (StringUtils.isBlank(shuntConfig)) {
            log.info("featureName={} 不开放流量，无任何分流配置", featureName);
            return false;
        }
        Map<String, ShuntConfigLevelDo> shuntConfigLevelDoMap = null;
        try {
            shuntConfigLevelDoMap = JsonUtils.parse(shuntConfig, new TypeReference<Map<String, ShuntConfigLevelDo>>() {});
            log.info("shuntConfigLevelDoMap={}", shuntConfigLevelDoMap);
        } catch (Exception e) {
            log.error("featureName={} 不开放流量，分流配置存在问题 error={}", featureName, e);
        }
        // 分流配置存在问题
        if (shuntConfigLevelDoMap == null) {
            log.info("featureName={} 不开放流量，分流配置存在问题", featureName);
            return false;
        }
        ShuntConfigLevelDo shuntConfigLevelDo = shuntConfigLevelDoMap.get(featureName);
        // 未配置分流策略
        if (shuntConfigLevelDo == null) {
            log.info("featureName={} 不开放流量，分流配置没有指定策略", featureName);
            return false;
        }
        // 系统级别分流配置
        Boolean systemLevel = shuntConfigLevelDo.getSystemLevel();
        if (Boolean.TRUE.equals(systemLevel)) {
            log.info("featureName={} 开放流量，进行系统级别分流 systemLevel={}", featureName, systemLevel);
            return true;
        }
        // 租户级别分流配置
        String tenantId = TenantContext.getTenantId();
        List<String> tenantLevelList = shuntConfigLevelDo.getTenantLevel();
        if (CollectionUtils.isNotEmpty(tenantLevelList)) {
            if (tenantLevelList.contains(tenantId)) {
                log.info("featureName={} 开放流量，租户级别分流包含当前租户 tenantLevelList={} tenantId={}", featureName, tenantLevelList, tenantId);
                return true;
            }
        }
        // 获取当前登录人信息
        BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
        log.info("当前登陆人信息 baseUserInfo={}", baseUserInfo);
        if (baseUserInfo == null || baseUserInfo.getOrgId() == null || StringUtils.isBlank(tenantId)) {
            log.info("featureName={} 不开放流量，无登陆人信息", featureName);
            return false;
        }
        // 组织级别分流配置
        String orgId = baseUserInfo.getOrgId();
        Map<String, List<String>> orgLevelMap = shuntConfigLevelDo.getOrgLevel();
        if (CollectionUtils.isNotEmpty(orgLevelMap)) {
            List<String> orgIdList = orgLevelMap.get(tenantId);
            if (CollectionUtils.isNotEmpty(orgIdList) && orgIdList.contains(orgId)) {
                log.info("featureName={} 开放流量，组织级别分流包括当前组织 orgIdList={} orgId={}", featureName, orgIdList, orgId);
                return true;
            }
        }
        // 用户级别分流配置
        String uid = baseUserInfo.getUid();
        Map<String, List<String>> uidLevelMap = shuntConfigLevelDo.getUidLevel();
        if (CollectionUtils.isNotEmpty(uidLevelMap)) {
            List<String> uidList = uidLevelMap.get(tenantId);
            if (CollectionUtils.isNotEmpty(uidList) && uidList.contains(uid)) {
                log.info("featureName={} 开放流量，用户级别分流包括当前用户 uidList={} uid={}", featureName, uidList, uid);
                return true;
            }
        }
        // 未任何配置
        log.info("featureName={} 不开放流量，不满足任何级别配置", featureName);
        return false;
    }

}
